package com.qpl.depression.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.qpl.depression.entity.Article;
import com.qpl.depression.entity.CategoryLevel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Desc
 * @Date 2021/5/17 16:27
 **/
public class PdfUtils {
    public static void ExportPdf(HttpServletResponse response, CategoryLevel categoryLevel, double scope, int paperSize, int time) throws Exception {
        //告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/pdf");
        //下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("抑郁症在线检测结果.pdf", "UTF-8"));
        //设置中文
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        Document document = new Document();
        PdfWriter.getInstance(document, response.getOutputStream());
        // 打开文档
        document.open();
        //设置文档标题
        document.addTitle("PDF");
        //设置文档作者
        document.addAuthor("秦培隆");
        document.addCreationDate();
        //设置关键字
        document.addKeywords("iText");
        document.addLanguage("中文");


        Image image1 = Image.getInstance("D:\\workspace\\depression\\src\\main\\resources\\static\\images\\logo.png");
        //设置图片位置的x轴和y周
//        image1.setAbsolutePosition(100f, 550f);
        //设置图片的宽度和高度
        image1.scaleAbsolute(200, 100);
        //将图片1添加到pdf文件中
        document.add(image1);



        // 4.添加一个内容段落
        Font titleFont = new Font(bfChinese, 26, Font.BOLD);
        titleFont.setColor(BaseColor.RED);
        Paragraph titleParagraph = new Paragraph("抑郁症在线检测结果", titleFont);
        titleParagraph.setAlignment(Element.ALIGN_CENTER);

        document.add(titleParagraph);
        document.add(new Paragraph(" "));


        Font firstLevel = new Font(bfChinese, 12, Font.NORMAL);


        Paragraph sizeParagraph = new Paragraph("一共做题："+paperSize, firstLevel);
        sizeParagraph.setAlignment(Element.ALIGN_LEFT);
        document.add(sizeParagraph);


        Paragraph scopeParagraph = new Paragraph("得分："+scope, firstLevel);
        scopeParagraph.setAlignment(Element.ALIGN_LEFT);
        document.add(scopeParagraph);

        time= time ==0 ? 1:time;
        Paragraph timeParagraph = new Paragraph("总共耗时："+time + " 分钟", firstLevel);
        timeParagraph.setAlignment(Element.ALIGN_LEFT);
        document.add(timeParagraph);
        document.add(new Paragraph(" "));






        Font adviceFont = new Font(bfChinese, 12, Font.NORMAL);
        Paragraph adviceParagraph = new Paragraph("诊断建议", adviceFont);
        adviceParagraph.setAlignment(Element.ALIGN_LEFT);
        document.add(adviceParagraph);
        //添加有序列表
        List orderedList = new List(List.UNORDERED);
        String advice = StringUtils.delHTMLTag(categoryLevel.getAdvice());
        for (String s : advice.split(";")) {
            orderedList.add(new ListItem(s, adviceFont));
        }
        document.add(orderedList);
        document.close();
    }
}
